
let app = getApp();
//let keys = '4UTBZ-PUWLX-UGS4R-TIK3C-WFL73-N4FWH';
let keys = 'GWRBZ-6UMYI-YHWGT-UFESG-R5KHH-L6BHJ';

const QQMapWX = require('../../libs/qqmap-wx-jssdk.js');
const qqmapsdk = new QQMapWX({
  key:keys
});
Page({

    /**
     * 页面的初始数据
     */
    data: {
        map:'log/map.png',
        scrollTop:0,
        intoView:'',
        top:0,
        showModel:0,
        showAsk:0
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        var id = options.id;
        if(id==undefined){
            id=1;
        }
        this.getStoreDetail(id);
        this.setData({
            id:id,
            width:app.globalData.windowWidth,
            height:app.globalData.windowHeight,
            statusBarHeight:app.globalData.statusBarHeight,
            navHeight:app.globalData.navHeight,
            marginTop:app.globalData.navTitleTop,
            bottom:app.globalData.bottom,
            domain:app.globalData.domain,
            imgUrl:app.globalData.img,
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        let that = this;
        this.setData({
            top:0
        })
        this.getTabBar().init();
        
       // this.getIndex(); 
       setTimeout(function(){ 
        //console.log("延时1秒后执行");
        that.getLocation(); 
      }, 3000);
        
         //this.getLocation();
       },

     goBack(){
        //wx.navigateBack();
        var pages = getCurrentPages();
        var currPage = pages[pages.length - 1];   //当前页面
        var prevPage = pages[pages.length - 2];  //上一个页面
       // prevPage.onRefresh()//刷新方法
        wx.navigateBack({
            delta:1
        });
      },
      call(e){
        var phone=e.currentTarget.dataset.phone;
        wx.makePhoneCall({
          phoneNumber: phone,
        })
      },
      goto:function(e){
        console.log(e);
        var url=e.currentTarget.dataset.url;
        var index=e.currentTarget.dataset.index;
        app.goto(url,index)
      // console.log(url);
    },
    intoView(e){
        var d = e.currentTarget.dataset.url;
        console.log(d);
        let that = this;
        let dd = this.data;
        let navHeight = dd.navHeight;
        let marginTop = dd.marginTop
        wx.createSelectorQuery().select('#'+d+'').boundingClientRect(function(rect){
            // rect.top即为目标元素的上边界距离页面顶部的距离
            console.log(rect);
            var top = rect.top;
            top = top - marginTop - navHeight-6;
            that.setData({
                top:top
            })
          }).exec();
          
   
          
    },

     getStoreDetail:function (id) {
        let that=this;
        var open=wx.getStorageSync('open');
        wx.request({
          url: app.globalData.domain+'wechat/store/getStoreDetail',
          data:{id:id,open_id:open.id},
          success:res=>{
            console.log('------------store detail-------');
              console.log(res.data);
              var data=res.data;
              var station=data.station;
              var phone=data.phone;
        
            
            
            that.setData({
                shareTitle:data.shareTitle,
                shareLogo:data.shareLogo,
                shareIndex:data.shareIndex,
                store:data.store,
                cate:data.cate

            })
            }
        })
    },
     getIndex:function(lat,lng,city,area){
        let that = this; 
        let d = this.data;
        wx.request({
          url: app.globalData.domain+'wechat/store/index',
          data:{form:2},
          success:res=>{
              console.log(res)
             if(res.statusCode==200){
                let data=res.data;
                console.log(data)
                that.setData({
                    cate:data.cate
  
                })
             }else{
                 app.showModal('系统错误')
             }
          }
        })
     },
     getLocation(){
         console.log('getlocation')
        let that = this;
        var store = this.data.store;
        console.log('-------store-----');
        console.log(store);
        wx.getLocation({
            success: function(res) {
                console.log(res)
                let lat = res.latitude;
                let lng = res.longitude
                var now_lat = wx.getStorageSync('now_lat');
                var now_lng = wx.getStorageSync('now_lng');
                
                if(lat == now_lat){
                    //console.log(lat);
                    let distance = wx.getStorageSync('distance');
                    that.setData({
                        distance:distance
                    })
                        wx.setStorageSync('now_lat', lat)
                        wx.setStorageSync('now_lng', lng)
                  }else{
                    that.getDistance(lat,lng,store.lat,store.lng);
                     wx.setStorageSync('now_lat',lat)
                     wx.setStorageSync('now_lng', lng)
                  }
                 // wx.setStorageSync('now_lat', res.latitude)
                 // wx.setStorageSync('now_lng', res.longitude)
               //that.getDistrict(res.latitude, res.longitude)
               //that.getIndex(res.latitude, res.longitude)
            
            },
            fail(err){
                wx.getSetting({
                     success: (res) => {
                       if (res.authSetting['scope.userLocation'] != undefined && res.authSetting['scope.userLocation'] != true) {//非初始化进入该页面,且未授权
                         wx.showModal({
                           title: '是否授权当前位置',
                           content: '需要获取您的地理位置，请确认授权，否则无法获取您所需数据',
                           success: function (res) {
                             if (res.cancel) {
                               wx.showToast({
                                 title: '授权失败',
                                 icon: 'success',
                                 duration: 1000
                               })
                               that.getIndex(0,0)
                             } else if (res.confirm) {
                               wx.openSetting({
                                 success: function (dataAu) {
                                   if (dataAu.authSetting["scope.userLocation"] == true) {
                                     wx.showToast({
                                       title: '授权成功',
                                       icon: 'success',
                                       duration: 1000
                                     })
                                     //再次授权，调用getLocationt的API
                                     //getLoca(that);
                                   } else {
                                     wx.showToast({
                                       title: '授权失败',
                                       icon: 'success',
                                       duration: 1000
                                     })
                                   }
                                 }
                               })
                             }
                           }
                         })
                       } 
                     }
                   })
            }
          });
    },
     gotoNavigate:function(e){
        console.log(e);
            
        var latitude=e.currentTarget.dataset.lat;
        var longitude=e.currentTarget.dataset.lng;       
       var position=e.currentTarget.dataset.name;
       var address=e.currentTarget.dataset.address;
      
       wx.navigateTo({
         url: '../map/navigate?latitude='+latitude+'&longitude='+longitude+'&position='+position+'&address='+address,
         success:res=>{
             console.log(res);
         }
       })
       
      },
      getDistance:function(latitude1,longitude1,latitude2,longitude2){
          //console.log(ee);
        let that=this;
        console.log('latitude1:'+latitude1+' longitude1:'+longitude1+' latitude2:'+latitude2+' longitude2:'+longitude2);
        wx.request({
          url: 'https://zq.browin.net.cn/wechat/home/getDistance',
          data:{
            from:latitude1+','+longitude1,
            to:latitude2+','+longitude2
          },
          success:res=>{
             // console.log(res);
            let distance =  res.data.distance;
             that.setData({
                 distance:distance
             })
             wx.setStorageSync('distance', distance)
          }
        })
      },
      brandModel(e){
          console.log(e);
          var id = e.currentTarget.dataset.id;
          let that = this; 
          let d = this.data;
          this.setData({
            showModel:1,
            showAsk:0,
          })
          return;
          wx.request({
            url: app.globalData.domain+'wechat/store/getModel',
            data:{form:2,id:id},
            success:res=>{
                console.log(res)
               if(res.statusCode==200){
                  let data=res.data;
                  console.log(data)
                  that.setData({
                      cate:data.cate
    
                  })
               }else{
                   app.showModal('系统错误')
               }
            }
          })
      },
      closeModel(){
          this.setData({
            showModel:0
          })
      },
      showAsk(){
          this.setData({
            showAsk:1,
            showModal:0
          })
      },
      closeAsk(){
        this.setData({
            showAsk:0
          })
      },
      
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})